# To change this template, choose Tools | Templates
# and open the template in the editor.

class Pearson
  #def initialize  end

  # Sommatoria
  def st(vet=[])
    sum = 0
    (0..vet.size-1).each{|i|
      sum = sum + vet[i]
    }
    return sum
  end

  # Sommatoria con argomento al quadrato
  def stq(vet=[])
    sum = 0
    (0..vet.size-1).each{|i|
      sum = sum + vet[i]**2}
    return sum
  end

  # Sommatoria a tra due vettori
  def std(vet1=[], vet2=[])
    sum = 0
    for i in 0..vet1.size-1
      sum = sum + (vet1[i]*vet2[i])
    end
    return sum
  end

 # Due Vettori di ingresso
  def ind_per(vet1=[], vet2=[])
    n = vet1.size
    exy = std(vet1,vet2).to_f
    ex = st(vet1).to_f
    ey = st(vet2).to_f
    ex2 = stq(vet1).to_f
    ey2 = stq(vet2).to_f

    return (exy-(ex*ey)/n)/Math.sqrt((ex2-(ex**2)/n)*(ey2-(ey**2)/n))

  end
  def ind_per_matrix(matrix, y)
      sim= []
  matrix.each{|x|
    sim.push(self.ind_per(x, y))
  }

  return sim
  end

  def test()
    a=[1,22,3]
    b=[22,5,6]
    p ind_per(a,b)
  end

end